package cn.beyond.vehicleManagement.controller;

import cn.beyond.vehicleManagement.pojo.DrivingLicense;
import cn.beyond.vehicleManagement.service.DrivingLicenseService;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.Location;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(urlPatterns = {"/drivingLicenseServlet"})
public class DrivingLicenseServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //定义全局变量
        DrivingLicenseService service=new DrivingLicenseService();
        resp.setContentType("application/json;charset=utf-8");
        String opr= req.getParameter("opr");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        DrivingLicense drivingLicense=new DrivingLicense();
        Map<String,Object> map = new HashMap<>();
        PrintWriter out= resp.getWriter();
        List<DrivingLicense> drivingLicenseList=new ArrayList<>();
        System.out.println(opr);
        switch (opr){
            case "addDrivingLicense":
                boolean addflag= addDrivingLicense(req,service);
                out.print(addflag);
                break;
            //删除驾照
            case "deleteDrivingLicense":
                int delId=Integer.parseInt(req.getParameter("Id"));
                boolean delflag=false;
                int delcount=service.deleteDrivingLicense(delId,"id");
                System.out.println(delcount);
                if(delcount>0){
                    delflag=true;
                }
                out.print(delflag);
                break;
            case "updateDrivingLicense":
                boolean updateflag=updateDrivingLicense(req,service);
                out.print(updateflag);
                break;
            case "selectDrivingLicense":
                int id= Integer.parseInt(req.getParameter("Id"));
                map.put("id",id);
                drivingLicense=service.selectDrivingLicenseByOne(drivingLicense,map);
                String JsondrivingLicense=JSON.toJSONString(drivingLicense);
                out.print(JsondrivingLicense);
                map.clear();
                break;
            case "listDrivingLicense":
                //列举出角色列表
                drivingLicenseList=service.selectUserByCondition(drivingLicense,map);
                String listJosn= JSON.toJSONString(drivingLicenseList);
                out.write(listJosn);
                map.clear();
                break;

        }
    }

    /**
     * 修改驾照信息
     * @param req
     * @param service
     * @return
     */
    private boolean updateDrivingLicense(HttpServletRequest req, DrivingLicenseService service) {

        int id= Integer.parseInt(req.getParameter("id"));
        String date1=req.getParameter("opening_date");
        String driving_license_No=req.getParameter("driving_license_No");
        String driver_id=req.getParameter("driver_id");
        String driver_school=req.getParameter("driver_school");
        int drive_type= Integer.parseInt(req.getParameter("drive_type"));
        DrivingLicense drivingLicense=new DrivingLicense(id,driving_license_No,
                driver_id,date1,driver_school,drive_type);

        int count=service.update(drivingLicense);
        if(count>0){
            return  true;
        }else {
            return  false;
        }

    }

    /**
     * 增加驾照信息
     * @param req
     * @param service
     * @return
     */
    private boolean addDrivingLicense(HttpServletRequest req, DrivingLicenseService service) {
       String date1=req.getParameter("opening_date");
       String driving_license_No=req.getParameter("driving_license_No");
                String driver_id=req.getParameter("driver_id");
                String driver_school=req.getParameter("driver_school");
                int drive_type= Integer.parseInt(req.getParameter("drive_type"));
                DrivingLicense drivingLicense=new DrivingLicense(null,driving_license_No,
                        driver_id,date1,driver_school,drive_type);

                int count=service.addDrivingLicense(drivingLicense);
                if(count>0){
                    return  true;
                }else {
                    return  false;
                }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
